<?php

require_once('../config/maptrick.conf.php');
require_once(SITE_ROOT . "config/session.php");

if (isset($_COOKIE['away'])) {
	$visited = unserialize(gzuncompress(base64_decode($_COOKIE['away'])));
}
else $visited = array();

if (isset($_COOKIE['home'])) {
	$hosted = unserialize(gzuncompress(base64_decode($_COOKIE['home'])));
}
else $hosted = array();

if (isset($_GET['special'])) {
	if ($_GET['special'] == 'true') {
		$img = imagecreatefromgif("worldmap_special.gif");
	}
	else $img = imagecreatefromgif("worldmap.gif");
}
else $img = imagecreatefromgif("worldmap_special.gif");

$ci = array(
	"1" => 1, #Sverige
	"2" => 2, #England
	"3" => 3, #Deutschland
	"4" => 4, #Italia
	"5" => 5, #France
	"6" => 6, #M�xico
	"7" => 7, #Argentina
	"8" => 8, #USA
	"9" => 9, #Norge
	"11" => 11, #Danmark
	"12" => 12, #Suomi
	"14" => 14, #Nederland
	"15" => 15, #Oceania
	"16" => 16, #Brasil
	"17" => 17, #Canada
	"18" => 18, #Chile
	"19" => 19, #Colombia
	"20" => 20, #India
	"21" => 21, #Ireland
	"22" => 22, #Nippon
	"23" => 23, #Per�
	"24" => 24, #Polska
	"25" => 25, #Portugal
	"26" => 26, #Scotland
	"27" => 27, #South Africa
	"28" => 28, #Uruguay
	"29" => 29, #Venezuela
	"30" => 30, #Hanguk
	"31" => 31, #Prathet Thai
	"32" => 32, #T�rkiye
	"33" => 33, #Misr
	"34" => 34, #China
	"35" => 35, #Rossiya
	"36" => 36, #Espa�a
	"37" => 37, #Rom�nia
	"38" => 38, #�sland
	"39" => 39, #�sterreich
	"44" => 44, #Belgi�
	"45" => 45, #Malaysia
	"46" => 46, #Schweiz
	"47" => 47, #Singapore
	"50" => 50, #Hellas
	"51" => 51, #Magyarorsz�g
	"52" => 52, #Ceska rep.
	"53" => 53, #Latvija
	"54" => 54, #Indonesia
	"55" => 55, #Philippines
	"56" => 56, #Eesti
	"57" => 57, #Srbija
	"58" => 58, #Hrvatska
	"59" => 59, #Hong Kong
	"60" => 60, #Chin. Taipei
	"61" => 61, #Cymru
	"62" => 62, #Bulgaria
	"63" => 63, #Israel
	"64" => 64, #Slovenija
	"66" => 66, #Lietuva
	"67" => 67, #Slovensko
	"68" => 68, #Ukraina
	"69" => 69, #Bosna i Herc
	"70" => 70, #Vietnam
	"71" => 71, #Pakistan
	"72" => 72, #Paraguay
	"73" => 73, #Ecuador
	"74" => 74, #Bolivia
	"75" => 75, #Nigeria
	"76" => 76, #F�royar
	"77" => 77, #Al Maghrib
	"79" => 79, #Saudi Arabia
	"80" => 80, #Tounes
	"81" => 81, #Costa Rica
	"83" => 83, #U.A.E.
	"84" => 84, #L�tzebuerg
	"85" => 85, #Iran
	"88" => 88, #Rep. Dom.
	"89" => 89, #Cyprus
	"91" => 91, #Belarus
	"93" => 93, #N. Ireland
	"94" => 94, #Jamaica
	"95" => 95, #Kenya
	"96" => 96, #Panam�
	"97" => 97, #Makedonija
	"98" => 98, #Shqiperia
	"99" => 99, #Honduras
	"100" => 100, #El Salvador
	"101" => 101, #Malta
	"102" => 102, #Kyrgyzstan
	"103" => 103, #Moldova
	"104" => 104, #Sakartvelo
	"105" => 105, #Andorra
	"106" => 106, #Al Urdun
	"107" => 107, #Guatemala
	"110" => 110, #Trinidad/T.
	"111" => 111, #Nicaragua
	"112" => 112, #Kazakhstan
	"113" => 113, #Suriname
	"117" => 117, #Liechtenst.
	"118" => 118, #Al Jazair
	"119" => 119, #Mongol Uls
	"120" => 120, #Lubnan
	"121" => 121, #S�n�gal
	"122" => 122, #Hayastan
	"123" => 123, #Bahrayn
	"124" => 124, #Barbados
	"125" => 125, #Cabo Verde
	"126" => 126, #C. d.Ivoire
	"127" => 127, #Kuwait
	"128" => 128, #Al Iraq
	"129" => 129, #Azerbaijan
	"130" => 130, #Angola
	"131" => 131, #Montenegro
	"132" => 132, #Bangladesh
	"133" => 133, #Al Yaman
	"134" => 134, #Uman
	"135" => 135, #Mozambique
	"136" => 136, #Brunei
	"137" => 137, #Ghana
	"138" => 138, #Cambodia/Kampuchea
	"139" => 139, #Benin
	"140" => 140, #Suriyah/Syria
	"141" => 141, #Dawlat Qata/Qatar
	"142" => 142, #Tanzania
	"143" => 143, #Uganda
	"144" => 144, #Maldives
	"145" => 145, #O'zbekiston / Uzbekistan
	"146" => 146, #Cameroon / Cameroon
	"147" => 147, #Cuba / Cuba
	"148" => 148, #Palestine / Palestine
);

imagecolortransparent($img, 42); //the sea is transparent
imagecolorset($img, 0, 90, 90, 90); //non HT countries
imagecolorset($img, 215, 45, 45, 45); //borders

foreach (array_keys($ci) as $index) {
	if (isset($visited[$index]) && isset($hosted[$index])) {
		imagecolorset($img, $ci[$index], 0, 200, 0);
	}
	else if (isset($visited[$index])) {
		imagecolorset($img, $ci[$index], 0, 0, 255);
	}
	else if (isset($hosted[$index])) {
		imagecolorset($img, $ci[$index], 200, 200, 0);
	}
	else imagecolorset($img, $ci[$index], 180, 180, 180); //non-visited existant countries
}

$src_img = imagecreatefromgif(SITE_ROOT . "images/map_logo.gif");
imagecopymerge($img, $src_img, 10, 525, 0, 0, 250, 100, 100);

if (isset($_GET['legend'])) {
	if ($_GET['legend'] == 'true') {
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$non_ht = imagecolorallocate($img, 90, 90, 90); //not in HT
$ht = imagecolorallocate($img, 180, 180, 180); //not visited
$away = imagecolorallocate($img, 0, 0, 255); //visited only
$home = imagecolorallocate($img, 200, 200, 0); //hosted only
$both = imagecolorallocate($img, 0, 200, 0); //both

imagefilledrectangle($img, 19, 359, 206, 491, $black); //legend border
imagefilledrectangle($img, 20, 360, 205, 490, $white); //legend background

ImageRectangle($img, 25, 365, 45, 385, $black);
ImageRectangle($img, 25, 390, 45, 410, $black);
ImageRectangle($img, 25, 415, 45, 435, $black);
ImageRectangle($img, 25, 440, 45, 460, $black);
ImageRectangle($img, 25, 465, 45, 485, $black);

ImageFilledRectangle($img, 26, 366, 44, 384, $non_ht); //not in HT
ImageFilledRectangle($img, 26, 391, 44, 409, $ht); //not visited
ImageFilledRectangle($img, 26, 416, 44, 434, $away); //visited only
ImageFilledRectangle($img, 26, 441, 44, 459, $home); //hosted only
ImageFilledRectangle($img, 26, 466, 44, 484, $both); //both

// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath(SITE_ROOT . 'fonts'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'verdana';

imagettftext($img, 14, 0, 50, 384, $black, $font, "not in HT");
imagettftext($img, 14, 0, 50, 409, $black, $font, "no friendly");
imagettftext($img, 14, 0, 50, 434, $black, $font, "visited");
imagettftext($img, 14, 0, 50, 459, $black, $font, "hosted");
imagettftext($img, 14, 0, 50, 484, $black, $font, "visited & hosted");
}
}

header("Content-type: image/png");
imagepng($img);

imagedestroy($img);
imagedestroy($src_img);

?>